/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-03-26
 * Time:23:11
 */

/**
 * BM4 合并两个排序的链表
 */

public class Merge {
    public ListNode Merge(ListNode list1, ListNode list2) {
        ListNode output = new ListNode(0);
        ListNode cur = output;
        ListNode cur1 = list1;
        ListNode cur2 = list2;

        while (cur1 != null && cur2 != null) {
            if (cur1.val <= cur2.val) {
                cur.next = cur1;
                cur1 = cur1.next;
            } else {
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        cur.next = cur1 != null ? cur1 : cur2;
        return output.next;
    }
}
